// @ts-ignore
/* eslint-disable */
import { request } from "@umijs/max";
import TokenManager from "@/token";
import type { API } from "@/types/api";

// 公共Token验证方法
// async function validateToken(navigate: any): Promise<boolean> {
//     const accessToken = TokenManager.getAccessToken();
//     const isInvalidAccessToken = [
//         null,
//         "",
//         " [object Promise]",
//         "null",
//     ].includes(accessToken);
//     // console.log("accessToken", accessToken);
//     // 如果access_token无效，则尝试刷新token
//     if (!isInvalidAccessToken) {
//         const refreshToken = TokenManager.getRefreshToken();
//         const isInvalidRefreshToken = [
//             null,
//             "",
//             " [object Promise]",
//             "null",
//         ].includes(refreshToken);
//         // console.log("refreshToken", refreshToken);
//         if (isInvalidRefreshToken) {
//             console.log("refresh_token过期！");
//             navigate("/login");
//             window.location.reload();
//         }
//         await postRefreshToken(navigate);
//         return false;
//     }
//     return true;
// }
/** 获取当前的用户信息 POST /api/core/user/user_info */
export async function currentUser(
    body: { uid: string },
    // navigate?: any,
): Promise<API.CurrentUserResult> {
    // await validateToken(navigate);
    return await request("/api/core/user/user_info", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + TokenManager.getAccessToken(),
        },
        data: body,
    });
}

/** 登录接口 POST /api/login/account */
export async function login(
    body: API.LoginParams,
    options?: { [key: string]: any },
) {
    return await request<API.LoginResult>("/api/core/user/login", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        data: body,
        ...(options || {}),
    });
}

/** 刷新accessToken DELETE /api/core/user/refresh_token */
export async function postRefreshToken(navigate: any): Promise<void> {
    const refresh_token = TokenManager.getRefreshToken();
    if (refresh_token === null) {
        console.log("refresh_token is null");
        navigate("/login");
        window.location.reload();
    }
    await request<Record<string, any>>("/api/core/user/refresh_token", {
        method: "POST",
        headers: {
            Authorization: "Bearer " + TokenManager.getRefreshToken(),
            "Content-Type": "application/json",
            Accept: "*/*",
        },
    })
        .then((res: any) => {
            TokenManager.saveAccessToken(res.message.access_token);
            // TokenManager.saveRefreshToken(res.message.refresh_token);
        })
        .catch((err: any) => {
            console.log("请求详情:", err);
            if (
                err.response.data.msg === "Token has expired" ||
                "Token has been revoked"
            ) {
                navigate("/login");
                window.location.reload();
            }
        });
}

/** 验证refresh_token是否可用 POST /api/core/user/verify_token */
export async function postVerifyToken(): Promise<boolean> {
    return await request<Record<string, any>>("/api/core/user/verify_token", {
        method: "POST",
        headers: {
            Authorization: "Bearer " + TokenManager.getRefreshToken(),
        },
        data: {
            refresh_token: TokenManager.getRefreshToken(),
        },
    })
        .then((res) => {
            if (res.status_code === "TOKEN_ACTIVE") {
                return true;
            }
            return false;
        })
        .catch((err) => {
            console.log("verify_token失败：", err);
            return false;
        });
}
